﻿using ImageDomain.Agg;
using ImageDomain.Services;
using OpenCvSharp;
using ScienceRpcServe.Protos;
using System;
using System.Threading.Tasks;

namespace ScienceRpcServe.Utils;

public static class SegServiceExtension
{
    public static Task<TagInfoResponse> ToTagInfoResponse(this IArmBoneSegAgg agg)
    {
        var rs = new TagInfoResponse();

        var tags = agg.CtImage!.Tags;

        var pn = "0010|0010";
        var ps = "0010|0040"; // sex
        var pa = "0010|1010"; // age
        var pp = "0018|5100";

        rs.PatientInfo.Add(tags.Keys.Contains(pn) ? tags[pn] : "");
        rs.PatientInfo.Add(tags.Keys.Contains(pa) ? tags[pa] : "");
        rs.PatientInfo.Add(tags.Keys.Contains(ps) ? tags[ps] : "");
        rs.PatientInfo.Add(tags.Keys.Contains(pp) ? tags[pp] : "");

        var st = "0008|0020";
        var md = "0008|0060";
        var mf = "0008|0070";
        rs.StudyInfo.Add(tags.Keys.Contains(st) ? tags[st] : "");
        rs.StudyInfo.Add(tags.Keys.Contains(md) ? tags[md] : "");
        rs.StudyInfo.Add(tags.Keys.Contains(mf) ? tags[mf] : "");
        rs.StudyInfo.Add(agg.CtImage.SeriesDescription);


        rs.ImageInfo.Add($"Space:{Math.Round(agg.CtImage.Spacing![0],4)},{Math.Round(agg.CtImage.Spacing![1],4)},{Math.Round(agg.CtImage.Spacing![2],4)}");
        rs.ImageInfo.Add($"WinMax:{agg.CtImage.WinAdjust.WindowMax}, WinMin:{agg.CtImage.WinAdjust.WindowMin}");


        return Task.FromResult(rs);
    }

    public static Task<ImageInfoResponse> ToImageInfoResponse(this IArmBoneSegAgg agg)
    {
        if (agg.CtImage == null) throw new Exception("尚未加载图像");
        var rs = new ImageInfoResponse
        {
            Width = (int)agg.CtImage.OriginSize![0],
            Height = (int)agg.CtImage.OriginSize[1],
            Count = (int)agg.CtImage.OriginSize[2],
            OriginWindowMax = agg.CtImage.OriginWindowMax,
            OriginWindowMin = agg.CtImage.OriginWindowMin,
            SliceMin = agg.CtImage.WinAdjust.WindowMin,
            SliceMax = agg.CtImage.WinAdjust.WindowMax,
            ImageFactor = agg.CtImage.ImageFactor,
            SeriesDescription = agg.CtImage.SeriesDescription ?? "No Tag Name"
        };
        rs.Spacing.AddRange(agg.CtImage.Spacing);
        return Task.FromResult(rs);
    }

    /// <summary>
    /// todo 需要进行一些测试
    /// </summary>
    /// <param name="agg"></param>
    /// <param name="index"></param>
    /// <param name="orientation"></param>
    /// <returns></returns>
    public static Task<MatSetResponse> ToMatSetResponse(this IArmBoneSegAgg agg, int index,ImageOrientation orientation)
    {
        var matRs = new MatSetResponse();

        switch (orientation)
        {
            case ImageOrientation.TransversePlane:
                matRs.Orientation = ImageOrientation.TransversePlane;
                matRs.CurrentIndex = index;
                matRs.Count = (int)agg.CtImage!.OriginSize![2];
                if (agg.CtImage != null)
                    matRs.OriginMat = agg.CtImage.GetAxSlice(index).ConverToOriginalByteString();
                //if (agg.TfccImage3d != null)
                //    matRs.TFCCMat = agg.TfccImage3d.GetAxSlice(index,true).ConvertToByteString();
                //if (agg.RadiusImage3d != null)
                //    matRs.RADIUSMat = agg.RadiusImage3d.GetAxSlice(index,true).ConvertToByteString();
                //if (agg.UlnaImage3d != null)
                //    matRs.ULNAMat = agg.UlnaImage3d.GetAxSlice(index, true).ConvertToByteString();
                if (agg.LablesImage3d != null)
                {
                    matRs.LableMat = agg.LablesImage3d.GetAxSlice(index, true).ConvertToByteString();
                }

                break;
            case ImageOrientation.SagittalPlane:
                matRs.Orientation = ImageOrientation.SagittalPlane;
                matRs.CurrentIndex = index;
                matRs.Count = (int)agg.CtImage!.OriginSize![0];
                if (agg.CtImage != null)
                    matRs.OriginMat = agg.CtImage.GetSagSlice(index).ConverToOriginalByteString();
                //if (agg.TfccImage3d != null)
                //    matRs.TFCCMat = agg.TfccImage3d.GetSagSlice(index,true).ConvertToByteString();
                //if (agg.RadiusImage3d != null)
                //    matRs.RADIUSMat = agg.RadiusImage3d.GetSagSlice(index, true).ConvertToByteString();
                //if (agg.UlnaImage3d != null)
                //    matRs.ULNAMat = agg.UlnaImage3d.GetSagSlice(index, true).ConvertToByteString();
                if (agg.LablesImage3d != null)
                    matRs.LableMat = agg.LablesImage3d.GetSagSlice(index, true).ConvertToByteString();
                break;
            case ImageOrientation.CoronalPlane:
                matRs.Orientation = ImageOrientation.CoronalPlane;
                matRs.CurrentIndex = index;
                matRs.Count = (int)agg.CtImage!.OriginSize![1];
                if (agg.CtImage != null)
                    matRs.OriginMat = agg.CtImage.GetCorSlice(index).ConverToOriginalByteString();
                //if (agg.TfccImage3d != null)
                //    matRs.TFCCMat = agg.TfccImage3d.GetCorSlice(index, true).ConvertToByteString();
                //if (agg.RadiusImage3d != null)
                //    matRs.RADIUSMat = agg.RadiusImage3d.GetCorSlice(index, true).ConvertToByteString();
                //if (agg.UlnaImage3d != null)
                //    matRs.ULNAMat = agg.UlnaImage3d.GetCorSlice(index, true).ConvertToByteString();
                if (agg.LablesImage3d != null)
                    matRs.LableMat = agg.LablesImage3d.GetCorSlice(index, true).ConvertToByteString();
                break;
        }

        return Task.FromResult(matRs);
    }

}
